package com.gusha.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.ReceivingAddressDao;
import com.gusha.model.ReceivingAddress;
import com.gusha.model.User;

@Service
@Transactional
public class ReceivingAddressService {
	@Autowired
	private ReceivingAddressDao receivingAddressDao;
	
	
	public Page<ReceivingAddress> findAll(Pageable pageable, ReceivingAddress receivingAddress) {
        @SuppressWarnings("serial")
        Page<ReceivingAddress> result = receivingAddressDao.findAll(new Specification<ReceivingAddress>() {
            @Override
            public Predicate toPredicate(Root<ReceivingAddress> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (receivingAddress.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),receivingAddress.getId()));
                }
                if (receivingAddress.getUser() != null) {
                    list.add(cb.equal(root.get("user").as(User.class),receivingAddress.getUser()));
                }
                if (receivingAddress.getContactPerson() != null) {
                    list.add(cb.equal(root.get("contactPerson").as(String.class),receivingAddress.getContactPerson()));
                }
                if (receivingAddress.getContactPhone() != null) {
                    list.add(cb.equal(root.get("contactPhone").as(String.class),receivingAddress.getContactPhone()));
                }
                if (receivingAddress.getContactAdress() != null) {
                    list.add(cb.equal(root.get("contactAdress").as(String.class),receivingAddress.getContactAdress()));
                }
                if (receivingAddress.getHouseNumber() != null) {
                    list.add(cb.equal(root.get("houseNumber").as(String.class),receivingAddress.getHouseNumber()));
                }
                if (receivingAddress.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),receivingAddress.getIsDelete()));
                }
                if (receivingAddress.getAddTime() != null) {
                    list.add(cb.equal(root.get("addTime").as(Date.class),receivingAddress.getAddTime()));
                }
               
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }
}
